//
//  SettingController.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/21.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "SettingController.h"
#import "LoginController.h"
#import "SettingCell.h"

#define cellIdentifier @"SettingCell"

@interface SettingController ()

@property(nonatomic,strong)UITableView      *listTable;
@property(nonatomic,strong)NSArray   *titleArr;

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"设置";
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    // Do any additional setup after loading the view.
    [self createTableView];
    [_listTable reloadData];
    
}

-(void)createTableView{
    _titleArr = @[@[@{@"title":@"账户管理",@"icon":@"icon_fav",@"code":@"1"},
                    @{@"title":@"隐私管理",@"icon":@"icon_fav",@"code":@"2"},
                    @{@"title":@"黑名单管理",@"icon":@"icon_fav",@"code":@"3"}],
                  @[@{@"title":@"消息通知",@"icon":@"icon_fav",@"code":@"4"},
                    @{@"title":@"保存路径",@"icon":@"icon_fav",@"code":@"5"},
                  @{@"title":@"清除缓存",@"icon":@"icon_fav",@"code":@"6"},
                    @{@"title":@"检查更新",@"icon":@"icon_fav",@"code":@"7"}],
                  @[@{@"title":@"关于Q友乐园",@"icon":@"icon_fav",@"code":@"8"},
                    @{@"title":@"发布规则",@"icon":@"icon_fav",@"code":@"9"},
                    @{@"title":@"用户协议",@"icon":@"icon_fav",@"code":@"10"},
                    @{@"title":@"喜欢Q友乐园，去评分",@"icon":@"icon_fav",@"code":@"11"},
                    @{@"title":@"将Q友乐园，推荐给朋友",@"icon":@"icon_fav",@"code":@"12"},
                    @{@"title":@"意见箱",@"icon":@"icon_fav",@"code":@"13"}]];
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    [_listTable setFrame:self.view.bounds];
    _listTable.tableFooterView = [self tableFooter];
    [_listTable setBackgroundColor:UIColor.whiteColor];
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    [_listTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

-(UIView *)tableFooter{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 65)];
    
    // 退出登录
    UIView * logOutView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 45)];
    logOutView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 1, ScreenWidth-20, 44);
    [btn setTitle:@"退出登录" forState:UIControlStateNormal];
    btn.titleLabel.font = FontSeventeen;
    
    [btn addTarget:self action:@selector(commitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:UIColorFromRGB(TextWhiteColor) forState:UIControlStateNormal];
    [btn setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    btn.layer.cornerRadius = 6.0f;
    
    [logOutView addSubview:btn];
    [bgView addSubview:logOutView];
    return bgView;
}

-(IBAction)commitButtonClick:(id)sender{
    [[AlertUtil shareInstance] showAlert:@"确定要退出登录吗?" message:@"" cancelTitle:@"取消" titleArray:@[@"确定"] viewController:self confirm:^(NSInteger buttonTag) {
        if(buttonTag==0){
            [UserUtil deleteUser];
            [PatientUtil deletePatient];
            
            BaseNaviController *navi = [[BaseNaviController alloc] initWithRootViewController:[[LoginController alloc] init]];
            self.view.window.rootViewController = navi;
            
//            [NetworkUtil GET:API_Login parameters:@{} success:^(id responseObject) {
//
//            } failure:^(NSError *error) {
//
//            }];
        }
    }];
}


#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleArr.count;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_titleArr.count == 0){
        return 0;
    }
    NSArray *items = _titleArr[section];
    return items.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingCell *cell = (SettingCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSArray *items = _titleArr[indexPath.section];
    [cell dataToView:items[indexPath.row]];
    cell.labLine.hidden = !(indexPath.row  == items.count-1);
    return cell;
}

// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    VipModel *model=[_listArray objectAtIndex:indexPath.section];
    NSDictionary *item = _titleArr[indexPath.section][indexPath.row];
    int code = [item[@"code"] intValue];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
